<template>
  <div></div>
</template>

<script>
import { mapState } from 'vuex';
import utils from '@/utils';
import { authService, menuService } from '@/services';

export default {
  name: 'redirect',
  computed: mapState({
    loginSysUserVo: (state) => state.loginSysUserVo,
    state: (state) => state,
  }),
  activated() {
    this.redirect();
  },
  methods: {
    redirect() {
      const permissionCode = JSON.parse(localStorage.getItem('permissionCodes'));
      if ( utils.isEmpty(permissionCode) || permissionCode.length === 0) {
        authService.logout().then(() => {
          this.$router.push({ name: 'login' });
        });
        return;
      }
      const menuList = menuService.getMenuList(permissionCode);
      if (menuList && menuList.length > 0) {
        if (menuList[0].children) {
          this.$router.push({ path: menuList[0].children[0].index });
        } else {
          this.$router.push({ path: menuList[0].index });
        }
      } else {
        authService.logout().then(() => {
          this.$router.push({ name: 'login' });
        });
        return;
      }
    },
  },
};
</script>
